//
//  RegisterView.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/3.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "RegisterView.h"
#import "RegularRestrictions.h"
#import "CountDownButton.h"
#import "DDbaseNetWork+Login.h"
#import "DDcacheManager.h"
#import "NSString+mobileEncryption.h"

@interface RegisterView ()<UITextFieldDelegate,GXJtextFieldDelegate>
@end

@implementation RegisterView

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createRegisterView];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sendSMScodeSuccess) name:KsendSMScodeSuccessNotification object:nil];
    }
    return self;
}

- (void)createRegisterView {
    
    self.signImg = [[UIImageView alloc] init];
    self.signImg.clipsToBounds = YES;
    self.signImg.image = [UIImage imageNamed:@"河南政务logo"];
    self.signImg.backgroundColor = [UIColor clearColor];
    [self addSubview:self.signImg];
    [self.signImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(76 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(300 * m6Scale, 80 * m6Scale));
    }];
    
    
    self.titleLab = [[UILabel alloc] init];
    ZWUserModel *model = [cacheManager getUserModel];
    NSString *moboleEntryctString = [NSString stringWithFormat:@"当前手机号: %@",[model.mobile encryption]];
    self.titleLab.text = NSLocalString(moboleEntryctString);
    self.titleLab.font = [UIFont systemFontOfSize:32 * m6Scale];
    self.titleLab.textColor = RGB(103, 103, 103);
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.hidden = YES;
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.mas_top).offset(160 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(KScreenWidth, 60 * m6Scale));
    }];
    
    
    
    
    self.phoneTF = [[GXJTextField alloc] initWithFrame:CGRectMake(74 * m6Scale, 254 * m6Scale, 600 * m6Scale, 80 * m6Scale)];
    self.phoneTF.placeholder = NSLocalString(@"请输入有效手机号");
    self.phoneTF.font = 28 * m6Scale;
    self.phoneTF.backgroundColor = RGB(243, 243, 243);
    self.phoneTF.layer.borderWidth = 1.0;
    self.phoneTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.phoneTF.layer.cornerRadius = 6.0 * m6Scale;
    self.phoneTF.clear = YES;
    self.phoneTF.delegate = self;
    [self addSubview:self.phoneTF];
    
    
    self.checkTF = [[GXJTextField alloc] initWithFrame:CGRectMake(74 * m6Scale, 374 * m6Scale,396 * m6Scale, 80 * m6Scale)];
    self.checkTF.placeholder = NSLocalString(@"请输入验证码");
    self.checkTF.font = 28 * m6Scale;
    self.checkTF.backgroundColor = RGB(243, 243, 243);
    self.checkTF.layer.borderWidth = 1.0;
    self.checkTF.layer.borderColor = RGB(203, 203, 203).CGColor;
    self.checkTF.layer.cornerRadius = 6.0 * m6Scale;
    self.checkTF.clear = YES;
    self.checkTF.delegate = self;
    [self addSubview:self.checkTF];
//    [self.checkTF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.phoneTF.mas_left);
//        make.top.mas_equalTo(self.phoneTF.mas_bottom).offset(40 * m6Scale);
//        make.size.mas_equalTo(CGSizeMake(396 * m6Scale, 80 * m6Scale));
//    }];
    
    
    
    self.confirmatBtn = [[UIButton alloc] init];
    [self.confirmatBtn setTitle:NSLocalString(@"获取验证码") forState:0];
    [self.confirmatBtn setTitleColor:[UIColor whiteColor] forState:0];
    self.confirmatBtn.titleLabel.font = [UIFont systemFontOfSize:24 * m6Scale];
    self.confirmatBtn.backgroundColor = APPbuttonDisableColor;
    self.confirmatBtn.layer.cornerRadius = 6 * m6Scale;
    [self.confirmatBtn addTarget:self action:@selector(clickConfirmatBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.confirmatBtn];
    self.confirmatBtn.enabled = NO;
    [self.confirmatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.checkTF.mas_right).offset(20 * m6Scale);
        make.centerY.mas_equalTo(self.checkTF.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(184 * m6Scale, 80 * m6Scale));
    }];
    
    self.pactLab = [[UILabel alloc] init];
    self.pactLab.textColor = APPColor;

    NSString *string =  NSLocalString(@"我已阅读并同意《河南政务服务网协议》");
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string];
    [str addAttribute:NSForegroundColorAttributeName value:RGB(203, 203, 203) range:NSMakeRange(0,7)];
    self.pactLab.attributedText = str;
    
    self.pactLab.font = [UIFont systemFontOfSize:24 * m6Scale];
    [self addSubview:self.pactLab];
    [self.pactLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.checkTF.mas_left);
        make.top.mas_equalTo(self.checkTF.mas_bottom).offset(20 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(500 * m6Scale, 36 * m6Scale));
    }];
    
    self.nextBtn = [[UIButton alloc] init];
    [self.nextBtn setTitle:NSLocalString(@"下一步") forState:0];
    self.nextBtn.titleLabel.font = [UIFont systemFontOfSize:36 * m6Scale];
    [self.nextBtn setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:0];
    self.nextBtn.backgroundColor = RGB(210, 235, 255);
    self.nextBtn.layer.cornerRadius = 10.0 * m6Scale;
    [self.nextBtn addTarget:self action:@selector(clickNextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.nextBtn];
    self.nextBtn.enabled = NO;
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.pactLab.mas_bottom).offset(64 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(600 * m6Scale, 80 * m6Scale));
    }];
    
}

- (void)GXJtextFieldChangeText:(GXJTextField *)textField {
    
    if ([textField isEqual:self.phoneTF]) {
        if ([RegularRestrictions IsPhoneNumber:self.phoneTF.text]) {
            self.confirmatBtn.enabled = YES;
            self.confirmatBtn.backgroundColor = APPColor;
        }else {
            self.confirmatBtn.enabled = NO;
            self.confirmatBtn.backgroundColor = APPbuttonDisableColor;
        }
    }
    
    if (self.checkTF.text.length > 0 && [RegularRestrictions IsPhoneNumber:self.phoneTF.text]) {
        self.nextBtn.enabled = YES;
        self.nextBtn.backgroundColor = APPColor;
    }else {
        self.nextBtn.enabled = NO;
        self.nextBtn.backgroundColor = APPbuttonDisableColor;
    }
}

- (void) clickConfirmatBtn {
    
    if ([RegularRestrictions IsPhoneNumber:self.phoneTF.text]) {
        if (self.isForgetPassword) {
            [DDNetWorkManager forgerPwdToSendSMScode:self.phoneTF.text controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
                [DDHudHelper showHudWithResult:result resultSuccess:^{
                    [[NSNotificationCenter defaultCenter]postNotificationName:KsendSMScodeSuccessNotification object:nil];
                    [SVProgressHUD showSuccessWithStatus:NSLocalString(@"验证码已发送")];
                }];
            } failure:^(NSError *error) {
            }];
        }else if (self.isResetMobile) {
            [DDNetWorkManager rebindMobileSedMSMcode:self.phoneTF.text token:[cacheManager userToken] controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
                [[NSNotificationCenter defaultCenter]postNotificationName:KsendSMScodeSuccessNotification object:nil];
                [SVProgressHUD showSuccessWithStatus:NSLocalString(@"验证码已发送")];
            } failure:^(NSError *error) {
            }];
        }else {
            [DDNetWorkManager sendMSMcode:self.phoneTF.text controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
                [DDHudHelper showHudWithResult:result resultSuccess:^{
                    [[NSNotificationCenter defaultCenter]postNotificationName:KsendSMScodeSuccessNotification object:nil];
                    [SVProgressHUD showSuccessWithStatus:NSLocalString(@"验证码已发送")];
                }];
            } failure:^(NSError *error) {
            }];
        }
    }else {
        [SVProgressHUD showErrorWithStatus:NSLocalString(@"请输入正确格式的手机号码")];
    }
}

- (void)sendSMScodeSuccess {
    [[CountDownButton standardCountDown] scheduledCountDownWithKey:@"login" timeInterval:60 create:YES countingDown:^(NSTimeInterval timerInterval) {
        self.confirmatBtn.backgroundColor = [UIColor lightGrayColor];
        self.confirmatBtn.enabled = NO;
        [self.confirmatBtn setTitle:[NSString stringWithFormat:@"%.0f秒后重新获取",timerInterval] forState:UIControlStateNormal];
    } finished:^(NSTimeInterval timerInterval) {
        self.confirmatBtn.backgroundColor = APPColor;
        [self.confirmatBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
        self.confirmatBtn.enabled = YES;
    }];
}

- (void) clickNextBtn {
    
    if (self.isResetMobile) {
        if ([self.delegate respondsToSelector:@selector(clickNextBtn)]) {
            [self.delegate clickNextBtn];
        }
    }else {
        [DDNetWorkManager checkSMScode:self.checkTF.text mobile:self.phoneTF.text controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
            [DDHudHelper showHudWithResult:result resultSuccess:^{
                if ([self.delegate respondsToSelector:@selector(clickNextBtn)]) {
                    [self.delegate clickNextBtn];
                }
            }];
        } failure:^(NSError *error) {
        }];
    }
}

@end
